/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Persistencia;

import Modelo.Funcionario;
import Modelo.Pessoa;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.classic.Session;

/**
 *
 * @author lucia
 */
class pessoa {
    
    Session sessao = null;
    
    public void add(Pessoa p) {
        
         sessao = HibernateUtil.getSessionFactory().openSession();
        try{
            sessao.beginTransaction();
            sessao.save(p);
            sessao.getTransaction().commit();
            
        }finally{
            sessao.close();
        }
               
        
    }

    public Funcionario getAluById(Long id) {
        
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Funcionario f = (Funcionario) sessao.get(Funcionario.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return f;
    }
    
    public void update (Pessoa p) {
        
        sessao = HibernateUtil.getSessionFactory().openSession();
        try {
            sessao.beginTransaction();
            sessao.update(p);
            sessao.getTransaction().commit();
        }
        finally {
            sessao.close();
        }
    }

    public List<Funcionario> list() {
        
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        List<Funcionario> lista = new ArrayList<Funcionario>();
        lista = sessao.createQuery("from Pessoa where DTYPE = 'Pessoa'").list();
        sessao.getTransaction().commit();
        sessao.close();
        return lista;
    }
 
    public boolean remove(Pessoa p) {
        
        sessao = HibernateUtil.getSessionFactory().openSession();
        try{
            sessao.beginTransaction();
            sessao.delete(p);
            sessao.getTransaction().commit();
        }
        finally{
            sessao.close();
            
        }
        return true;
    }
}
